package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.account.AccountWithdrawInfo;
import com.tencreat.domain.account.MallUser;
import com.tencreat.domain.account.dto.AdminAuditResultDTO;
import com.tencreat.domain.mall.StoreWithdrawAudit;
import com.tencreat.domain.mall.vo.AuditResultVO;
import com.tencreat.domain.mall.vo.RrsAuditResultVO;
import com.tencreat.service.account.IAccountWithdrawInfoService;
import com.tencreat.service.account.IMallUserService;
import com.tencreat.service.account.IStoreWithdrawAuditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 店铺提现记录Controller
 *
 * @author tencreat
 * @date 2022-03-17
 */
@Api(tags="店铺提现记录api")
@RestController
@RequestMapping("/mall/storeAudit")
public class AdminStoreWithdrawAuditController extends BaseController {
    @Autowired
    private IStoreWithdrawAuditService storeWithdrawAuditService;
    @Resource
    private IAccountWithdrawInfoService withdrawInfoService;
    @Resource
    private IMallUserService mallUserService;
    /**
     * 查询店铺提现记录列表
     */
    @ApiOperation(value="查询店铺提现记录列表")
    @PreAuthorize("@ss.hasPermi('system:audit:list')")
    @PostMapping("/list")
    public HttpResult<IPage<StoreWithdrawAudit>> list(@RequestBody  StoreWithdrawAudit storeWithdrawAudit) {
        QueryWrapper<StoreWithdrawAudit> wrapper = Wrappers.query(storeWithdrawAudit);
        Page<StoreWithdrawAudit> page =  new Page<>(storeWithdrawAudit.getPageNum(), storeWithdrawAudit.getPageSize());
        wrapper.eq(storeWithdrawAudit.getUuid() != null,"uuid",storeWithdrawAudit.getUuid());//商家id
        wrapper.eq(storeWithdrawAudit.getMobile() != null,"mobile",storeWithdrawAudit.getMobile());//手机号
        wrapper.eq(storeWithdrawAudit.getName() != null,"name",storeWithdrawAudit.getName());//商家名称
        wrapper.eq(storeWithdrawAudit.getWithdrawType() != null,"withdraw_type",storeWithdrawAudit.getWithdrawType());//提现方式
        wrapper.ge(storeWithdrawAudit.getResult() != null, "result", storeWithdrawAudit.getResult());//审核状态
        //创建时间
        wrapper.ge(storeWithdrawAudit.getStartTime() != null, "create_time", storeWithdrawAudit.getStartTime());
        wrapper.ge(storeWithdrawAudit.getEndTime() != null, "create_time", storeWithdrawAudit.getEndTime());
        //审核时间
        wrapper.ge(storeWithdrawAudit.getAuditStartTime() != null, "audit_time", storeWithdrawAudit.getAuditStartTime());
        wrapper.ge(storeWithdrawAudit.getAuditEndTime() != null, "audit_time", storeWithdrawAudit.getAuditEndTime());
        wrapper.orderByDesc("result=0").orderByDesc("create_time");
        return HttpResult.ok(storeWithdrawAuditService.page(page, wrapper));
    }

    /**
     * 导出店铺提现记录列表
     */
    @ApiOperation(value="导出店铺提现记录列表")
    @PreAuthorize("@ss.hasPermi('system:audit:export')")
    @Log(title = "财务管理>提现审核-货款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody StoreWithdrawAudit storeWithdrawAudit) {
        QueryWrapper<StoreWithdrawAudit> wrapper = Wrappers.query(storeWithdrawAudit);
        Page<StoreWithdrawAudit> page =  new Page<>(1, Integer.MAX_VALUE);
        wrapper.orderByDesc("result=0").orderByDesc("create_time");
        wrapper.ge(storeWithdrawAudit.getStartTime() != null, "create_time", storeWithdrawAudit.getStartTime());
        wrapper.le(storeWithdrawAudit.getEndTime() != null, "create_time", storeWithdrawAudit.getEndTime());
        List<StoreWithdrawAudit> list = storeWithdrawAuditService.page(page, wrapper).getRecords();
        list.forEach(item ->{
            item.setCreateTime2(item.getCreateTime());
        });
        ExcelUtil<StoreWithdrawAudit> util = new ExcelUtil<StoreWithdrawAudit>(StoreWithdrawAudit.class);
        return util.exportExcel(list, "audit");
    }

    /**
     * 获取店铺提现记录详细信息
     */
    @ApiOperation(value="获取店铺提现记录详细信息")
    @PreAuthorize("@ss.hasPermi('system:audit:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<StoreWithdrawAudit> getInfo(@RequestBody  ReqIdParam id) {
        StoreWithdrawAudit storeWithdrawAudit = storeWithdrawAuditService.selectStoreWithdrawAuditById(id);
        String mobile = storeWithdrawAudit.getMobile();
        MallUser user = mallUserService.lambdaQuery().eq(MallUser::getMobile, mobile).one();
        if(user == null ){
            return HttpResult.ok(storeWithdrawAudit);
        }
        AccountWithdrawInfo accountWithdrawInfo = withdrawInfoService.lambdaQuery().eq(AccountWithdrawInfo::getUserId, user.getUuid()).oneOpt().orElse(new AccountWithdrawInfo());
        String phone = "";
        String name = "";
        String bankName = "";
        if (storeWithdrawAudit.getWithdrawType() == 1) {
            phone = accountWithdrawInfo.getWechatPhone();
            name = accountWithdrawInfo.getWechatName();
            storeWithdrawAudit.setQr(accountWithdrawInfo.getWechatQr());
        } else if (storeWithdrawAudit.getWithdrawType() == 2) {
            phone = accountWithdrawInfo.getAlipayPhone();
            name = accountWithdrawInfo.getAlipayName();
            storeWithdrawAudit.setQr(accountWithdrawInfo.getAlipayQr());
        } else {
            phone = accountWithdrawInfo.getBankCode();
            bankName = accountWithdrawInfo.getBankName();
            name = accountWithdrawInfo.getCardName();
        }
        storeWithdrawAudit.setPhone(phone);
        storeWithdrawAudit.setName(name);
        storeWithdrawAudit.setBankName(bankName);
        return HttpResult.ok(storeWithdrawAudit);
    }

    /**
     * 新增店铺提现记录
     */
    @ApiOperation(value="新增店铺提现记录")
    @PreAuthorize("@ss.hasPermi('system:audit:add')")
    @Log(title = "店铺提现记录", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  StoreWithdrawAudit storeWithdrawAudit) {
        return to(storeWithdrawAuditService.insertStoreWithdrawAudit(storeWithdrawAudit));
    }

    /**
     * 修改店铺提现记录
     */
    @ApiOperation(value="修改店铺提现记录")
    @PreAuthorize("@ss.hasPermi('system:audit:edit')")
    @Log(title = "店铺提现记录", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  StoreWithdrawAudit storeWithdrawAudit) {
        return to(storeWithdrawAuditService.updateStoreWithdrawAudit(storeWithdrawAudit));
    }

    /**
     * 删除店铺提现记录
     */
    @ApiOperation(value="删除店铺提现记录")
    @PreAuthorize("@ss.hasPermi('system:audit:remove')")
    @Log(title = "店铺提现记录", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(storeWithdrawAuditService.deleteStoreWithdrawAuditByIds(ids));
    }


    @ApiOperation(value = "审核")
    @Log(title = "财务管理>提现审核-货款", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/auditResult")
    public HttpResult<Boolean> auditResult(@RequestBody AdminAuditResultDTO param) {
        storeWithdrawAuditService.auditResult(param);
        return to(true);
    }

    @ApiOperation(value = "金额统计")
    @PostMapping(value = "/statistics")
    public HttpResult<RrsAuditResultVO> statistics() {
        RrsAuditResultVO result = storeWithdrawAuditService.priceStatistics();
        return HttpResult.ok(result);
    }

    @ApiOperation(value = "金额统计(新)")
    @GetMapping(value = "/statisticsV2")
    public HttpResult<AuditResultVO> statisticsV2() {
        AuditResultVO result = storeWithdrawAuditService.priceStatisticsV2();
        return HttpResult.ok(result);
    }
}
